package com.topisv.tms.web;

import java.util.List;
import java.util.Map;

import javax.servlet.ServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springside.modules.web.Servlets;

import com.topisv.tms.entity.AreaGroup;
import com.topisv.tms.entity.GoodsShelf;
import com.topisv.tms.entity.PipeStock;
import com.topisv.tms.entity.Storage;
import com.topisv.tms.entity.StoragePartition;
import com.topisv.tms.entity.StoragePipe;
import com.topisv.tms.service.AreaGroupService;
import com.topisv.tms.service.GoodsShelfService;
import com.topisv.tms.service.StoragePartitionService;
import com.topisv.tms.service.StoragePipeService;
import com.topisv.tms.service.StorageService;

/**
 * 货架管理的Controller, 使用Restful风格的Urls:
 * 
 * 
 * @author Arthur
 */
@Controller
@RequestMapping(value = "/storagePipe")
public class StoragePipeController {


	@Autowired
	private StoragePipeService storagePipeService;
	
	@Autowired
	private StoragePartitionService storagePartitionService;
	
	@Autowired
	private GoodsShelfService goodsShelfService;
	
	@RequestMapping(value = "")
	public String list(	Model model, ServletRequest request) {
		List<StoragePipe> storagePipes = storagePipeService.getAllStoragePipe();
		model.addAttribute("storagePipes", storagePipes);
		return "storagepartition/storagePartitionList";
	}

	@RequestMapping(value = "create", method = RequestMethod.GET)
	public String createForm(Model model) {
		model.addAttribute("storagePartition", new StoragePartition());
		model.addAttribute("action", "create");
		return "storagepartition/storagePartitionForm";
	}

	@RequestMapping(value = "create", method = RequestMethod.POST)
	public String create(@Valid StoragePipe storagePipe, RedirectAttributes redirectAttributes) {
		storagePipeService.saveStoragePipe(storagePipe);
		redirectAttributes.addFlashAttribute("message", "创建货架成功");
		return "redirect:/goodsShelf/";
	}
	
	@RequestMapping(value = "addPipe", method = RequestMethod.POST)
	public void create(@RequestParam(value = "partition_code", required = true) String partition_code, 
			@RequestParam(value = "goodsShelf_num", required = true) int goodsShelf_num,
			@RequestParam(value = "goodsShelf_code", required = true) String goodsShelf_code) {
		StoragePartition sp = storagePartitionService.findByCode(partition_code);
		GoodsShelf gs = goodsShelfService.findByCode(goodsShelf_code);
		int rowsnum = gs.getRowsnum();
		int colsnum = gs.getColsnum();
		
		int pipenum = sp.getPipe_num() + 1;
		String pipe_code = "";
		if(pipenum < 10)
		{
			pipe_code = "0" + pipe_code;
		}
		else if( pipenum >= 10 && pipenum < 100)
		{
			pipe_code = "" + pipe_code;
		}
		
		StoragePipe storagePipe = new StoragePipe();
		storagePipe.setPipe_code(sp.getPartition_code() + pipe_code);
		storagePipe.setPipe_name(pipe_code);
		storagePipe.setShelf_num(goodsShelf_num+"");
		storagePipe.setShelf_type(gs.getShelf_code());
		storagePipe.setPipe_index(pipenum+"");
		
		
		
		for(int i=0; i<goodsShelf_num; i++)
		{
			int num = i+1;
			String shelf_code = "";
			if( num < 10)
			{
				shelf_code = "00" + num;
			}
			else if( num >= 10 && num < 100)
			{
				shelf_code = "0" + num;
			}
			for(int rows=0; rows<rowsnum; rows++)
			{
				String row_code = "" + rows+1;
				for(int cols=0; cols<colsnum; cols++)
				{
					String cols_code = "" + cols+1;
					PipeStock pipeStock = new PipeStock();
					pipeStock.setPartition_code(sp.getPartition_code());
					pipeStock.setAreagroup_code(sp.getAreagroup_code());
					pipeStock.setCustomer_code("");
					pipeStock.setPartition_code(sp.getPartition_code());
					pipeStock.setStock_name(row_code + cols_code);
					pipeStock.setStock_code(storagePipe.getPipe_code()+ "-" + row_code + cols_code);
					pipeStock.setSku_id("");
					pipeStock.setPipe_code(storagePipe.getPipe_code());
					pipeStock.setStock_name(row_code + cols_code);
					pipeStock.setStock_status("0");
					pipeStock.setStock_type("0");
					pipeStock.setStorage_code(sp.getStorage_code());
					pipeStock.setShelf_code(shelf_code);
				}
			}
		}
	}

	@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
	public String updateForm(@PathVariable("id") Long id, Model model) {
		
		model.addAttribute("action", "create");
		return "storagepartition/storagePartitionForm";
	}

	@RequestMapping(value = "update", method = RequestMethod.POST)
	public String update(@Valid @ModelAttribute("storage") GoodsShelf goodsShelf,RedirectAttributes redirectAttributes) {
		
		redirectAttributes.addFlashAttribute("message", "修改货架成功");
		return "redirect:/goodsShelf/";
	}

	@RequestMapping(value = "delete/{id}")
	public String delete(@PathVariable("id") Long id, RedirectAttributes redirectAttributes) {
		storagePipeService.deleteStoragePipe(id);
		redirectAttributes.addFlashAttribute("message", "删除货架成功");
		return "redirect:/goodsShelf/";
	}

	/**
	 * 使用@ModelAttribute, 实现Struts2 Preparable二次部分绑定的效果,先根据form的id从数据库查出Task对象,再把Form提交的内容绑定到该对象上。
	 * 因为仅update()方法的form中有id属性，因此本方法在该方法中执行.
	 */
	@ModelAttribute("preloadStoragePipe")
	public StoragePipe getStoragePipe(@RequestParam(value = "id", required = false) Long id) {
		if (id != null) {
			return storagePipeService.getStoragePipe(id);
		}
		return null;
	}
}